float4x4 World;
float4x4 View;
float4x4 Projection;

//float FarClip;

texture Texture;
sampler diffuseSampler = sampler_state
{
    Texture = (Texture);
    MAGFILTER = LINEAR;
    MINFILTER = LINEAR;
    MIPFILTER = LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float3 Normal   : NORMAL0;
    float2 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position         : POSITION0;
    float2 TexCoord         : TEXCOORD0;
    float3 Normal           : TEXCOORD1;
    float2 Depth            : TEXCOORD2;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(float4(input.Position.xyz, 1), World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    output.TexCoord = input.TexCoord;

    output.Normal = mul(input.Normal, World);
	output.Normal = mul(input.Normal, View);

    output.Depth.x = output.Position.z;
    output.Depth.y = output.Position.w;
	//output.Depth.x = viewPosition.z;
	//output.Depth.y = 0;

    return output;
}

struct PixelShaderOutput
{
    half4 Color     : COLOR0;
    half4 Normal    : COLOR1;
    half4 Depth     : COLOR2;
};

PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
    PixelShaderOutput output = (PixelShaderOutput)0;

    //output.Color = 1.0;
    output.Color = tex2D(diffuseSampler, input.TexCoord);
    output.Color.a = 1.0;

    output.Normal.rgb = 0.5 * (normalize(input.Normal) + 1.0);
    output.Normal.a = 1.0;

    // depth in screen space.
    output.Depth = input.Depth.x / input.Depth.y;

    // depth as a factor to far clip
	//output.Depth = -input.Depth.x / FarClip;

    output.Depth.a = 1.0;

    return output;
}

technique Render
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
